<?php
include_once ("../Login/databaseconnection.php");

$TeaName=$_POST['TeaName'];
$name=explode(" ",$TeaName);
$fn=$name[0];
$ln=$name[1];
$results= mysql_query("SELECT * FROM teacher Where First_Name='$fn' AND Last_Name='$ln'");
if($row = mysql_fetch_array($results))
{
 $T_NIC_Number=$row['T_NIC_Number'];
}
  if($TeaName!="")
	   {
      if($_SESSION['Access_Type']=="Administrator")
   {
   $result=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
   $resulttra=mysql_query("SELECT * FROM training_details WHERE T_NIC_Number='$T_NIC_Number'");
$resultres=mysql_query("SELECT * FROM resource_team WHERE T_NIC_Number='$T_NIC_Number'");
   	 	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 $Role=$row['Role'];
  $T_NIC_Number=$row['T_NIC_Number'];
  $School_Id=$row['School_Id'];
  $First_Name=$row['First_Name'];
  $Last_Name=$row['Last_Name'];
  $Date_Of_Birth=$row['Date_Of_Birth'];
  $Gender=$row['Gender'];
  $Nature_Of_Appointment=$row['Nature_Of_Appointment'];
  $Service=$row['Service'];
  $Achievements=$row['Achievements_Awards'];
  $Address=$row['Address'];
  $Mobile_Tp_Number=$row['Mobile_Tp_Number'];
  $Home_Tp_Number=$row['Home_Tp_Number'];
  $Office_Tp_Number=$row['Office_Tp_Number'];
  $Appointment_Date=$row['Appointment_Date'];
  $Appointed_Subject=$row['Appointed_Subject'];
		 }
		 if(mysql_num_rows($result)==0)
   {
	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   else
   {
$del=mysql_query("DELETE FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
  if($resulttra)
   {
		 while($rowtra=mysql_fetch_array($resulttra))
		 {
		 	$Training_Id=$rowtra['Training_Id'];
		mysql_query("DELETE FROM training_details WHERE T_NIC_Number='$T_NIC_Number' AND Training_Id='$Training_Id'");
		 }
		 }
		 	 if($resultres)
   {
		 while($rowres=mysql_fetch_array($resultres))
		 {
	    $Training_Id=$rowres['Training_Id'];
		mysql_query("DELETE FROM resource_team WHERE T_NIC_Number='$T_NIC_Number' AND Training_Id='$Training_Id'");
		 }
		 }
 	  if(mysql_num_rows(mysql_query("SELECT * FROM training_details WHERE Teacher_NIC='$T_NIC_Number'"))!=0)
	  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from training details!";
  }  
  if(mysql_num_rows(mysql_query("SELECT * FROM resource_team WHERE T_NIC_Number='$T_NIC_Number'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from resource team!";
   }
  }
if(!$del)
  {
  $Role='';
  $T_NIC_Number='';
  $School_Id='';
  $First_Name='';
  $Last_Name='';
  $Date_Of_Birth='';
  $Gender='';
  $Nature_Of_Appointment='';
  $Service='';
  $Achievements='';
  $Address='';
  $Mobile_Tp_Number='';
  $Home_Tp_Number='';
  $Office_Tp_Number='';
  $Appointment_Date='';
  $Appointed_Subject='';
 $_SESSION['error']="Sorry unable to delete this record!";
  }

  }
  }
  }
    if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   $get_Sch1=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
   if($rowsch1=mysql_fetch_array($get_Sch1))
{
  $School_Id=$rowsch1['School_Id'];

   if($School_Id==$Own_School_Id)
	{
	$result=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
	$resulttra=mysql_query("SELECT * FROM training_details WHERE T_NIC_Number='$T_NIC_Number'");
$resultres=mysql_query("SELECT * FROM resource_team WHERE T_NIC_Number='$T_NIC_Number'");
		 	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 $Role=$row['Role'];
		  $T_NIC_Number=$row['T_NIC_Number'];
  $School_Id=$row['School_Id'];
  $First_Name=$row['First_Name'];
  $Last_Name=$row['Last_Name'];
  $Date_Of_Birth=$row['Date_Of_Birth'];
  $Gender=$row['Gender'];
  $Nature_Of_Appointment=$row['Nature_Of_Appointment'];
  $Service=$row['Service'];
  $Achievements=$row['Achievements_Awards'];
  $Address=$row['Address'];
  $Mobile_Tp_Number=$row['Mobile_Tp_Number'];
  $Home_Tp_Number=$row['Home_Tp_Number'];
  $Office_Tp_Number=$row['Office_Tp_Number'];
  $Appointment_Date=$row['Appointment_Date'];
  $Appointed_Subject=$row['Appointed_Subject'];
		 }
		   if($resulttra)
   {
		 while($rowtra=mysql_fetch_array($resulttra))
		 {
		 if($rowtra['Training_Id']!="")
		 {
		 if(empty($Training_Id))
		 {
		 $Training_Id=$rowtra['Training_Id'];
		 }
		 else
		 {
		  $Training_Id=$Training_Id.",".$rowtra['Training_Id'];
		 }
		 }
		 if($rowtra['Duration']!="")
		 {
		  if(empty($Duration))
		 {
		 $Duration=$rowtra['Duration'];
		 }
		 else
		 {
		  $Duration=$Duration.",".$rowtra['Duration'];
		 }
		 }
		 }
		 }
		 	 if($resultres)
   {
		 while($rowres=mysql_fetch_array($resultres))
		 {
		 if($rowtra['From_Date']!="")
		 {
		  if(empty($From_Date))
		 {
		 $From_Date=$rowtra['From_Date'];
		 }
		 else
		 {
		  $From_Date=$From_Date.",".$rowtra['From_Date'];
		 }		
		 }
				 if($rowtra['Member_Id']!="")
		 {
		  echo $Member_Id=$rowres['Member_Id'];
		 }
		 }
		 }
	    if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   else
   {
   $Accessed_By=$_SESSION['NIC_Number'];
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
   if($row=mysql_fetch_array($result))
   {
   $Role=$row['Role'];
   $T_NIC_Number=$row['T_NIC_Number'];
	$School_Id=$row['School_Id'];
	$First_Name=$row['First_Name'];
	$Last_Name=$row['Last_Name'];
	$Date_Of_Birth=$row['Date_Of_Birth'];
  $Gender=$row['Gender'];
  $Nature_Of_Appointment=$row['Nature_Of_Appointment'];
  $Service=$row['Service'];
  $A_Qua=$row['Acadamic_Qualifications'];
  $Profe_Qualifications=$row['Professional_Qualifications'];
  $Achieve=$row['Achievements_Awards'];
  $Address=$row['Address'];
  $Mobile_Tp_Number=$row['Mobile_Tp_Number'];
  $Home_Tp_Number=$row['Home_Tp_Number'];
  $Office_Tp_Number=$row['Office_Tp_Number'];
  $Appointment_Date=$row['Appointment_Date'];
  $Appointed_Subject=$row['Appointed_Subject'];
   $Last_Updated_Date=$row['Last_Updated_Date']; 
   $Specialised_In=$row['Specialised_In'];   
   }
   	 $sql=mysql_query("INSERT INTO deleted_teacher(Specialised_In,Role,T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Academic_Qualifications,Professional_Qualifications,Achievements_Awards,Service,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Appointment_Date,Appointed_Subject,Deleted_date,Accessed_By,Last_Updated_Date,Training_Id,Duration,Member_Id,From_Date) VALUES('$Specialised_In','$Role','$T_NIC_Number','$School_Id','$First_Name','$Last_Name','$Date_Of_Birth','$Gender','$Nature_Of_Appointment','$A_Qua','$Profe_Qualifications','$Achieve','$Service','$Address','$Mobile_Tp_Number','$Home_Tp_Number','$Office_Tp_Number','$Appointment_Date','$Appointed_Subject','$t_d','$Accessed_By','$Last_Updated_Date','$Training_Id','$Duration','$Member_Id','$From_Date')");
if($sql){
		$del=mysql_query("DELETE FROM teacher WHERE T_NIC_Number='$T_NIC_Number'");
	if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }
if(!$del)
  {
    $Role='';
   $T_NIC_Number='';
  $School_Id='';
  $First_Name='';
  $Last_Name='';
  $Date_Of_Birth='';
  $Gender='';
  $Nature_Of_Appointment='';
  $Service='';
  $Achievements='';
  $Address='';
  $Mobile_Tp_Number='';
  $Home_Tp_Number='';
  $Office_Tp_Number='';
  $Appointment_Date='';
  $Appointed_Subject='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_teacher WHERE T_NIC_Number='$T_NIC_Number' AND Deleted_Date='$t_d'");
  }
	  if($resulttra)
   {
		  while($rowtra=mysql_fetch_array($resulttra))
		 {
		 	$Training_Id=$rowtra['Training_Id'];
		mysql_query("DELETE FROM training_details WHERE T_NIC_Number='$T_NIC_Number' AND Training_Id='$Training_Id'");
		 }
		 }
		 	 if($resultres)
   {
		 while($rowres=mysql_fetch_array($resultres))
		 {
	    $Training_Id=$rowres['Training_Id'];
		mysql_query("DELETE FROM resource_team WHERE T_NIC_Number='$T_NIC_Number' AND Training_Id='$Training_Id'");
		 }
		 }
 	  if(mysql_num_rows(mysql_query("SELECT * FROM training_details WHERE Teacher_NIC='$T_NIC_Number'"))!=0)
	  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from training details!";
  }
  
  if(mysql_num_rows(mysql_query("SELECT * FROM resource_team WHERE T_NIC_Number='$T_NIC_Number'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from resource team!";
   }
  }
     if(!$sql)
{
  $Role='';
$T_NIC_Number='';
  $School_Id='';
  $First_Name='';
  $Last_Name='';
  $Date_Of_Birth='';
  $Gender='';
  $Nature_Of_Appointment='';
  $Service='';
  $Achievements='';
  $Address='';
  $Mobile_Tp_Number='';
  $Home_Tp_Number='';
  $Office_Tp_Number='';
  $Appointment_Date='';
  $Appointed_Subject='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
  }
  }
  else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
   }
  } 
  else
   {
   $_SESSION['errornotallo']="This person's record is not registered into Teacher's record!";
   }
}  
  else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  }
   }
  }
   if($TeaName=="")
	   {
	   $_SESSION['errorin']="Please select the Teacher's Name!";
		 }
   ?>
